package com.yingwei.mining.simulator;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.yingwei.mining.control.Data;
import com.yingwei.mining.model.businessobject.Truck;

public class Simulator
{
    private static final Logger logger = LoggerFactory
	    .getLogger(Simulator.class);
    private static final Data data = Data.getData();
    private static final long serialVersionUID = 1L;
    private List<TruckMemory> truckMemorys = new ArrayList<TruckMemory>();

    public static void main(String[] args)
    {
	Simulator drawer = new Simulator();
	drawer.run();
    }

    // Create an editor.
    public Simulator()
    {
    }

    public void run()
    {
	data.init();
	data.postInitProcess();
	for (Truck truck : data.getTrucks())
	{
	    truckMemorys.add(new TruckMemory(truck.getId(),-1,-1));
	}
	try
	{
	    while (true)
	    {
		data.clear();
		simulateTrucks();
		Thread.sleep(1000);
	    }
	}
	catch (InterruptedException e)
	{
	    logger.error("error in simulate trucks", e);
	}
	catch (Exception e)
	{
	    logger.error("error in simulate trucks", e);
	}
	finally
	{
	    data.close();
	}
    }

    public void simulateTrucks()
    {
	for (TruckMemory truckMemory : truckMemorys )
	{
	    truckMemory.copy( (new TruckRobot(truckMemory)).run() );
	}
    }
}
